﻿using Microsoft.Extensions.Caching.Memory;
using Modules.MSentEvent;
using Modules.Units;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Sorting.App.Controls;
using Sorting.App.Models;
using Sorting.App.Models.Enum;
using Sorting.App.Services;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using Osklib.Wpf;
using SortingWorkerService;

namespace Sorting.App.ViewModels
{

    /// <summary>
    /// 登录界面视图模型
    /// </summary>
    public class LoginViewModel : BindableBase
    {
        IEventAggregator _ea;
        LoginService _loginService;

        public LoginViewModel(
            IEventAggregator ea,
            LoginService loginService
            )
        {
            _ea = ea;
            _loginService = loginService;

        }


        private DelegateCommand minCmd;
        public ICommand MinCmd => minCmd ??= new DelegateCommand(PerformMinCmd);

        private void PerformMinCmd()
        {
            //_ea.GetEvent<ViewModelSentEvent>().Publish(new MessageModel() { 

            //    Code=EnumMessageType.ME100,
            //    Message="登录界面最小化"
            //});


            //唤醒软键盘
            //Osklib.OnScreenKeyboard.Show();

        }

        private DelegateCommand loginCmd;
        public ICommand LoginCmd => loginCmd ??= new DelegateCommand(PerformLoginCmd);

        private async void PerformLoginCmd()
        {
            this.IsWating = true;

             if (string.IsNullOrEmpty(this.UserName))
            {
                TostHelper.ShowWarning("用户名不能为空");
                this.IsWating = false;
                return;
            }

            if (string.IsNullOrEmpty(this.Password))
            {
                TostHelper.ShowWarning("密码不能为空");
                this.IsWating = false;
                return;
            }
            AppsettingUnits.Appseeting.Local = false;
            if (this.UserName == "12345" && this.Password == "1")
            {
                AppsettingUnits.Appseeting.Local = true;
            }
            else
            {
                var res = await _loginService.Login(this.UserName, UtilHelper.MD5Encrypt32(this.Password));

                if (!res.succeeded)
                {
                    TostHelper.ShowWarning(res.errors);
                    this.IsWating = false;
                    return;
                }

                TostHelper.ShowSucess("登录成功");
                AppsettingUnits.Token = res.data.Token;
                AppsettingUnits.CaCheUserInfo = res.data;
               
            }
            this.IsWating = false;
            _ea.GetEvent<ViewModelSentEvent>().Publish(new MessageModel()
            {
                Code = EnumMessageType.ME101,
                Message = "登录成功"
            });
        }

        private DelegateCommand closeCmd;
        public ICommand CloseCmd => closeCmd ??= new DelegateCommand(PerformCloseCmd);

        /// <summary>
        /// 关闭当前应用
        /// </summary>
        private void PerformCloseCmd()
        {


            var res = Process.GetProcessesByName("SortingWorkerService").FirstOrDefault();
            res.Kill();

            Process.GetCurrentProcess().Kill();
            Application.Current.Shutdown();

        }

        private string password = "";

        public string Password
        {
            get
            {
                return password;
            }
            set
            {
                password = value;
                this.RaisePropertyChanged("password");
            }
        }
        private string userName = "";

        public string UserName { get => userName; set => SetProperty(ref userName, value); }

        private bool isWating = false;

        public bool IsWating { get => isWating; set => SetProperty(ref isWating, value); }

        private bool popIsOpen = false;

        public bool PopIsOpen { get => popIsOpen; set => SetProperty(ref popIsOpen, value); }

        private DelegateCommand gotFocusUserName;
        public ICommand GotFocusUserName => gotFocusUserName ??= new DelegateCommand(PerformGotFocusUserName);

        private void PerformGotFocusUserName()
        {

            PopIsOpen = true;
            PopIsTwoOpen=false;
        }

        private DelegateCommand submitCilck;
        public ICommand SubmitCilck => submitCilck ??= new DelegateCommand(PerformSubmitCilck);

        private void PerformSubmitCilck()
        {
            PopIsOpen = false;
            PopIsTwoOpen = false;
        }

        private bool popIsTwoOpen;

        public bool PopIsTwoOpen { get => popIsTwoOpen; set => SetProperty(ref popIsTwoOpen, value); }

        private DelegateCommand gotFocusPwd;
        public ICommand GotFocusPwd => gotFocusPwd ??= new DelegateCommand(PerformGotFocusPwd);

        private void PerformGotFocusPwd()
        {
            PopIsTwoOpen = true;
            PopIsOpen=false;
        }
    }
}
